f(char*a,char*b){return *a-*b;}
char c[9<<9],d[9<<9],e[9<<9];
main(i,j,w)
{
	for(gets(c);gets(c);puts(e))
	{
		gets(d);
		qsort(c,strlen(c),1,f);
		qsort(d,strlen(d),1,f);
		
		for(w=i=j=0;c[i]&&d[j];j++)
		{
			while(c[i]<d[j])i++;
			if(c[i]==d[j])
				e[w++]=c[i++];
		}
		e[w]=0;
	}
}
